buzzer and light controlled by arduino with a LDR

This project consists in being able to change de color of the light and the sound of the buzzer depending on the light the LDR receives using an arduino code

This is the arduino code I have used linking the code of the light and the speaker


   int valueLDR=1;
  int ledRed=9;
  int ledGreen=10;
  int ledBlue=11;
  int pinLDR=A0;
  int speaker=6;
  int freq;
  void setup() {
  // put your setup code here, to run once:
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);
  //analogReference(EXTERNAL);
  Serial.begin(9600);
  }
  
  void loop() {
  Serial.println(valueLDR);
  delay(1);
  // put your main code here, to run repeatedly:
  valueLDR=analogRead(pinLDR);
  
  int ledRed = map(valueLDR,50,500,0,255);
  int ledGreen = map(valueLDR,50,500,255,0);
  int ledBlue = map(valueLDR,50,500,0,128);
  int freq = map(valueLDR,50,500,261,494);
  analogWrite(9,ledRed);
  analogWrite(10,ledGreen);
  analogWrite(11,ledBlue);
  tone(speaker,freq,10);
  }
    

At first the two codes didn't work well together and we didn't know why, because the two of them worked well lonely but when they were together they didn't. We tried lots of things to see were the problem when finally we saw that the code we were using had a potenciometer in the code, but we weren't using one, so we only had to remove it and the code was working perfectly.

this is the line we had to remove if you have the same problem: analogReference(EXTERNAL);

Once we had finished the project, we decided to put a multimeter, to mesure the total resistence of the circuit with diferent resistors in order to know which was the best to use.

Here you can see the results:

330Ω±5%:

10000Ω±5%:

22000Ω±5%:

In conclusion, the greatest resistor we tried on was the first one "330Ω±5%". This is because with that one the LDR will be able to mark larger variety of numbers than if we put one of the two other options

Here you can see a video we did showing our results